//
//  NSDictionary+KCDefaultValue.m
//  KCDriver
//
//  Created by bjkcwl-fth on 2020/9/16.
//  Copyright © 2020 bjkcwl. All rights reserved.
//

#import "NSDictionary+KCDefaultValue.h"

@implementation NSDictionary (KCDefaultValue)

- (void)logUnexpectedObject:(id)object expecting:(NSString *)expectingClass {
    NSLog(@"unexpected object %@ of class %@ when expecting %@",
         object,
         [[object class] description],
         expectingClass);
}

- (id)getObjectNotNullForKey:(NSString *)key {
    if (![key isKindOfClass:[NSString class]]) {
        return nil;
    }
    
    id object = [self objectForKey:key];
    if (object == nil || [object isKindOfClass:[NSNull class]]) {
        return nil;
    }
    return object;
}


- (BOOL)getBoolForKey:(NSString *)key defaultValue:(BOOL)defaultValue {
    id object = [self getObjectNotNullForKey:key];
    if (object == nil) {
        return defaultValue;
    } else if ([object isKindOfClass:[NSNumber class]]) {
        return [object boolValue];
    } else if ([object isKindOfClass:[NSString class]]) {
        return [object boolValue];
    }
    [self logUnexpectedObject:object expecting:@"BOOL"];
    return defaultValue;
}

- (BOOL)getBoolForKey:(NSString *)key {
    return [self getBoolForKey:key defaultValue:NO];
}


- (NSInteger)getIntegerForKey:(NSString *)key defaultValue:(NSInteger)defaultValue {
    id object = [self getObjectNotNullForKey:key];
    if (object == nil) {
        return defaultValue;
    } else if ([object isKindOfClass:[NSNumber class]]) {
        return [object integerValue];
    } else if ([object isKindOfClass:[NSString class]]) {
        return [object integerValue];
    }
    [self logUnexpectedObject:object expecting:@"NSInteger"];
    return defaultValue;
}

- (NSInteger)getIntegerForKey:(NSString *)key {
    return [self getIntegerForKey:key defaultValue:0];
}


- (double)getDoubleForKey:(NSString *)key defaultValue:(double)defaultValue {
    id object = [self getObjectNotNullForKey:key];
    if (object == nil) {
        return defaultValue;
    } else if ([object isKindOfClass:[NSNumber class]]) {
        return [object doubleValue];
    } else if ([object isKindOfClass:[NSString class]]) {
        return [object doubleValue];
    }
    [self logUnexpectedObject:object expecting:@"double"];
    return defaultValue;
}

- (double)getDoubleForKey:(NSString *)key {
    return [self getDoubleForKey:key defaultValue:0.0];
}


- (NSString *)getStringForKey:(NSString *)key defaultValue:(NSString *)defaultValue {
    id object = [self getObjectNotNullForKey:key];
    if (object == nil) {
        return defaultValue;
    } else if ([object isKindOfClass:[NSString class]]) {
        return object;
    } else if ([object isKindOfClass:[NSNumber class]]) {
        return [object stringValue];
    }
    [self logUnexpectedObject:object expecting:@"NSString"];
    return defaultValue;
}

- (NSString *)getStringForKey:(NSString *)key {
    return [self getStringForKey:key defaultValue:@""];
}


- (NSArray *)getArrayForKey:(NSString *)key defaultValue:(NSArray *)defaultValue {
    id object = [self getObjectNotNullForKey:key];
    if (object == nil) {
        return defaultValue;
    } else if ([object isKindOfClass:[NSArray class]]) {
        return object;
    }
    [self logUnexpectedObject:object expecting:@"NSArray"];
    return defaultValue;
}

- (NSArray *)getArrayForKey:(NSString *)key {
    return [self getArrayForKey:key defaultValue:@[]];
}


- (NSDictionary *)getDictionaryForKey:(NSString *)key defaultValue:(NSDictionary *)defaultValue {
    id object = [self getObjectNotNullForKey:key];
    if (object == nil) {
        return defaultValue;
    } else if ([object isKindOfClass:[NSDictionary class]]) {
        return object;
    }
    [self logUnexpectedObject:object expecting:@"NSDictionary"];
    return defaultValue;
}

- (NSDictionary *)getDictionaryForKey:(NSString *)key {
    return [self getDictionaryForKey:key defaultValue:@{}];
}

@end
